package cn.iocoder.yudao.module.iot.service.devicerel;

import cn.iocoder.yudao.module.iot.dal.dataobject.warnpolicy.WarnPolicyDO;
import cn.iocoder.yudao.module.iot.enums.IotErrorCodeConstants;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.iot.controller.admin.devicerel.vo.*;
import cn.iocoder.yudao.module.iot.dal.dataobject.devicerel.DeviceRelDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.iot.dal.mysql.devicerel.DeviceRelMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;

/**
 * 报警器和气阀绑定 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class DeviceRelServiceImpl implements DeviceRelService {

    @Resource
    private DeviceRelMapper deviceRelMapper;

    @Override
    public Integer createDeviceRel(DeviceRelSaveReqVO createReqVO) {
        // 插入
        DeviceRelDO deviceRel = BeanUtils.toBean(createReqVO, DeviceRelDO.class);
        deviceRelMapper.insert(deviceRel);
        // 返回
        return deviceRel.getId();
    }

    @Override
    public void updateDeviceRel(DeviceRelSaveReqVO updateReqVO) {
        // 校验存在
        validateDeviceRelExists(updateReqVO.getId());
        // 更新
        DeviceRelDO updateObj = BeanUtils.toBean(updateReqVO, DeviceRelDO.class);
        deviceRelMapper.updateById(updateObj);
    }

    @Override
    public void deleteDeviceRel(Integer id) {
        // 校验存在
        validateDeviceRelExists(id);
        // 删除
        deviceRelMapper.deleteById(id);

    }

    @Override
    public void batchDeleteDeviceRel(List<Integer> ids) {
        deviceRelMapper.deleteBatchIds(ids);
    }

    private void validateDeviceRelExists(Integer id) {
        if (deviceRelMapper.selectById(id) == null) {
            throw exception(IotErrorCodeConstants.DEVICE_REL_NOT_EXISTS);
        }
    }

    @Override
    public DeviceRelDO getDeviceRel(Integer id) {
        return deviceRelMapper.selectById(id);
    }

    @Override
    public DeviceRelDO getDeviceRelByDeviceName(String deviceName) {
        return deviceRelMapper.getDeviceRelByDeviceName(deviceName);
    }

    @Override
    public PageResult<DeviceRelDO> getDeviceRelPage(DeviceRelPageReqVO pageReqVO) {
        return deviceRelMapper.selectPage(pageReqVO);
    }

    @Override
    public DeviceRelDO getDeviceRelByvalveCode(String valveCode) {
        return deviceRelMapper.getDeviceRelByvalveCode(valveCode);
    }

    @Override
    public Long getDeviceCountByDeviceName(String deviceName) {
        return deviceRelMapper.getDeviceCountByDeviceName(deviceName);
    }

    @Override
    public Long getDeviceCountByvalveCode(String valveCode) {
        return deviceRelMapper.getDeviceCountByvalveCode(valveCode);
    }


}